package com.woniu.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Ma.Lei
 * @description
 * @date 2023/2/3 11:15
 */
@Data
@ApiModel("统一响应对象")
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    // 响应状态码最好在一个地方统一定义好，不要在程序的每个位置自己定义，很难维护
    @ApiModelProperty("响应状态码")
    private Integer code;
    @ApiModelProperty("响应消息")
    private String message;
    @ApiModelProperty("响应数据")
    private T data;

    private static <T> Result<T> common(int code,String message,T data){
        return new Result<>(code,message,data);
    }

    public static <T> Result<T> ok(){
        return common(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),null);
    }

    public static <T> Result<T> ok(T data){
        return common(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> Result<T> ok(int code,String message){
        return common(code,message,null);
    }

    public static <T> Result<T> ok(int code,String message,T data){
        return common(code, message, data);
    }

    public static <T> Result<T> error(int code){
        return common(code,ResultCode.ERROR.getMessage(),null);
    }

    public static <T> Result<T> error(T data){
        return common(ResultCode.ERROR.getCode(),ResultCode.ERROR.getMessage(),data);
    }

    public static <T> Result<T> error(int code,String message){
        return common(code,message,null);
    }

    public static <T> Result<T> error(int code,String message,T data){
        return common(code,message,data);
    }
}
